using Backoffice.Domain.Entities.App;
using BackOffice.Domain.Entities.App;
using Microsoft.EntityFrameworkCore;

namespace Backoffice.Infrastructure.Data;


public class BackOfficeDbContext : DbContext
{
    public BackOfficeDbContext(DbContextOptions options) : base(options) { }

    public DbSet<AppRolePermission> AppRolePermissions { get; set; }
    public DbSet<AppRole> AppRoles { get; set; }

    public DbSet<AppPermission> AppPermissions { get; set; }
    public DbSet<AppUserRole> AppUserRoles { get; set; }

    public DbSet<AppUser> AppUsers { get; set; }
    public DbSet<AppSystemSetting> AppSystemSettings { get; set; }
    public DbSet<AppSystem> AppSystems { get; set; }
    public DbSet<AppMenu> AppMenus { get; set; }
    public DbSet<AppArticle>     Articles     { get; set; }
    public DbSet<AppTag>         Tags         { get; set; }
    public DbSet<AppArticleTag>  ArticleTags  { get; set; }
    public DbSet<AppFile>       Files   { get; set; }
    public DbSet<AppFolder>     Folders { get; set; }
    public DbSet<AppFileShare>  Shares  { get; set; }
    public DbSet<AppFileAcl>    Acls    { get; set; }
    public BackOfficeDbContext() { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        base.OnModelCreating(modelBuilder);
        var defaultTime = new DateTime(2025, 7, 31, 6, 34, 5, DateTimeKind.Utc);

        modelBuilder.Entity<AppUser>().HasData
        (
            new
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Username = "admin",
                PasswordHash = "$2a$11$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", // 对应密码 "password"
                Email = "admin@example.com",
                Phone = "+1234567890",
                FirstName = "Admin",

                LastName = "User",
                LastLoginAt = defaultTime,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()



            }
        );
        modelBuilder.Entity<AppRole>().HasData
        (
            new
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Name = "Administrator",
                Description = "System Administrator",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppUserRole>().HasData
        (
            new
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                UserId = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                RoleId = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppPermission>().HasData
        (
            new
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Code = "view_dashboard",
                Name = "View Dashboard",
                Description = "Permission to view the dashboard",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppRolePermission>().HasData
        (
            new
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                RoleId = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                PermissionId = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppSystemSetting>().HasData
        (
            new
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Key = "SiteName",
                Value = "Universal Admin System",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppMenu>().HasData
        (
            new
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Name = "Dashboard",
                Url = "/dashboard",
                Icon = "dashboard-icon",
                ParentId = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Order = 1,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppArticle>().HasData
        (
            new
            {
                Id = Guid.Parse("22222222-2222-2222-2222-222222222222"),
                Title = "Clean Architecture 入门",
                Content = "富文本内容...",
                Category = "技术",
                Tags = "DDD,Clean,入门",
                Status = ArticleStatus.Published, // 枚举转 int
                ParentId = (Guid?)null,           // 无父级
                Order = 1,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            },
            new
            {
                Id = Guid.Parse("33333333-3333-3333-3333-333333333333"),
                Title = "微服务实践",
                Content = "微服务落地指南...",
                Category = "架构",
                Tags = "微服务,进阶",
                Status = ArticleStatus.Draft,
                ParentId = (Guid?)null,
                Order = 2,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );

        modelBuilder.Entity<AppTag>().HasData
        (
            new
            {
                Id = Guid.Parse("44444444-4444-4444-4444-444444444444"),
                Name = "DDD",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            },
            new
            {
                Id = Guid.Parse("55555555-5555-5555-5555-555555555555"),
                Name = "Clean",
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );

        modelBuilder.Entity<AppArticleTag>().HasData
        (
            new
            {
                Id = Guid.Parse("66666666-6666-6666-6666-666666666666"),
                ArticleId = Guid.Parse("22222222-2222-2222-2222-222222222222"),
                TagId = Guid.Parse("44444444-4444-4444-4444-444444444444"),
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            },
            new
            {
                Id = Guid.Parse("77777777-7777-7777-7777-777777777777"),
                ArticleId = Guid.Parse("22222222-2222-2222-2222-222222222222"),
                TagId = Guid.Parse("55555555-5555-5555-5555-555555555555"),
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppFolder>().HasData
        (
            new
            {
                Id = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),
                Name = "示例文件夹",
                Description = "系统初始化示例目录",
                ParentId = (Guid?)null,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppFile>().HasData
        (
            new
            {
                Id = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),
                FileName = "README.md",
                FileSize = 1024L,
                FileExtension = ".md",
                MimeType = "text/markdown",
                RelativePath = "uploads/2025/07/README.md",
                Description = "项目说明文档",
                FolderId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),
                UploadedBy = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            },
            new
            {
                Id = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"),
                FileName = "logo.png",
                FileSize = 20480L,
                FileExtension = ".png",
                MimeType = "image/png",
                RelativePath = "uploads/2025/07/logo.png",
                Description = "公司Logo",
                FolderId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),
                UploadedBy = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted = false,
                CreateBy = "system",
                UpdateBy = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppFileShare>().HasData
        (
            new
            {
                Id       = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"),
                FileId   = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),
                SharedBy = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                SharedTo = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                ExpireAt = DateTime.MaxValue.ToUniversalTime(),  // 永不过期
                Mode     = ShareMode.ReadWrite,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted  = false,
                CreateBy   = "system",
                UpdateBy   = "system",
                RowVersion = Array.Empty<byte>()
            }
        );
        modelBuilder.Entity<AppFileAcl>().HasData
        (
            new
            {
                Id     = Guid.Parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"),
                FileId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"),
                UserId = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Access = BackOffice.Domain.Entities.App.FileAccess.All,
                CreatedAt = defaultTime,
                UpdatedAt = defaultTime,
                IsDeleted  = false,
                CreateBy   = "system",
                UpdateBy   = "system",
                RowVersion = Array.Empty<byte>()
            }
        );

        // 暂时注释掉初始日志数据，避免迁移问题
        // modelBuilder.Entity<AppSystem>().HasData(...);
        
    }
}
